﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Win32Wrapper.Win32
{
    public class Win32Helper
    {
        public delegate bool EnumDelegate(IntPtr hWnd, int lParam);

        #region unbearbeitet

        #region user32.dll

        [DllImport("user32.dll")]
        public static extern bool AdjustWindowRect(ref _Rect lpRect, uint dwStyle, bool bMenu);

        [DllImport("user32.dll")]
        public static extern bool AdjustWindowRect(ref _Rect lpRect, WindowStyles dwStyle, bool bMenu);

        [DllImport("user32.dll")]
        public static extern bool AdjustWindowRectEx(ref _Rect lpRect, uint dwStyle, bool bMenu, uint dwExStyle);

        [DllImport("user32.dll")]
        public static extern bool AdjustWindowRectEx(ref _Rect lpRect, WindowStyles dwStyle, bool bMenu, ExtendedWindowStyles dwExStyle);

        [DllImport("user32.dll")]
        public static extern bool AllowSetForegroundWindow(uint dwProcessId);

        [DllImport("user32.dll")]
        public static extern bool AnimateWindow(IntPtr hwnd, uint dwTime , uint dwFlags);

        [DllImport("user32.dll")]
        public static extern bool AnimateWindow(IntPtr hwnd, uint dwTime, AnimationFlags dwFlags);

        [DllImport("user32.dll")]
        public static extern bool AnyPopup();

        [DllImport("user32.dll")]
        public static extern uint ArrangeIconicWindows(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern IntPtr BeginDeferWindowPos(int nNumWindows);

        [DllImport("user32.dll")]
        public static extern bool BringWindowToTop(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern int CascadeWindows(IntPtr hWnd, uint wHow, _Rect lpRect, uint cKids, IntPtr[] lpKids);
        
        [DllImport("user32.dll")]
        public static extern int CascadeWindows(IntPtr hWnd, uint wHow, _Rect lpRect, CascadeFlags cKids, IntPtr lpKids);

        [DllImport("user32.dll")]
        public static extern bool ChangeWindowMessageFilter(uint message, uint dwFlag);

        [DllImport("user32.dll")]
        public static extern bool ChangeWindowMessageFilter(uint message, MessageFilterFlags dwFlag);

        [DllImport("user32.dll")]
        public static extern bool ChangeWindowMessageFilterEx(IntPtr hWnd, uint message, uint action, ref _ChangeFilterStruct pChangeFilterStruct);
          
        [DllImport("user32.dll")]
        public static extern bool ChangeWindowMessageFilterEx(IntPtr hWnd, uint message, ChangeFilterActionFlags action, ref _ChangeFilterStruct pChangeFilterStruct);

        [DllImport("user32.dll")]
        public static extern IntPtr ChildWindowFromPoint(IntPtr hWndParent, _Point  Point);

        [DllImport("user32.dll")]
        public static extern IntPtr ChildWindowFromPointEx(IntPtr hWndParent, _Point pt, uint uFlags);

        [DllImport("user32.dll")]
        public static extern IntPtr ChildWindowFromPointEx(IntPtr hWndParent, _Point pt, ChildWindowFromPointExFlags uFlags);

        [DllImport("user32.dll")]
        public static extern bool CloseWindow(IntPtr hwnd);

        [DllImport("user32.dll")]
        public static extern IntPtr CreateWindow(String lpClassName, String lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

        [DllImport("user32.dll")]
        public static extern IntPtr CreateWindow(String lpClassName, String lpWindowName, WindowStyles dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

        [DllImport("user32.dll")]
        public static extern IntPtr CreateWindowEx(uint dwExStyle, String lpClassName, String lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

        [DllImport("user32.dll")]
        public static extern IntPtr CreateWindow(ExtendedWindowStyles dwExStyle, String lpClassName, String lpWindowName, WindowStyles dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

        [DllImport("user32.dll")]
        public static extern bool DeferWindowPos(IntPtr hWinPosInfo, IntPtr hwnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

        [DllImport("user32.dll")]
        public static extern bool DestroyWindow(IntPtr hwnd);

        [DllImport("user32.dll")]
        public static extern bool EndDeferWindowPos(IntPtr hWinPosInfo);

        [DllImport("user32.dll")]
        public static extern bool EndTask(IntPtr hWnd, bool fShutDown, bool fForce);

        [DllImport("user32.dll")]
        public static extern bool EnumChildWindows(IntPtr hWndParent, EnumDelegate lpEnumFunc, IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern bool EnumThreadWindows(uint dwThreadId, EnumDelegate lpEnumFunc, IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern bool EnumWindows(EnumDelegate lpEnumFunc, IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, String lpClassName, String lpWindowName);

        [DllImport("user32.dll")]
        public static extern bool GetAltTabInfo(IntPtr hWnd, int iItem, out _AltTabInfo pati, out StringBuilder pszItemText, uint cchItemText);

        [DllImport("user32.dll")]
        public static extern bool GetAncestor(IntPtr hWnd , uint gaFlags);

        [DllImport("user32.dll")]
        public static extern bool GetAncestor(IntPtr hWnd, GetAncestorFlags gaFlags);

        [DllImport("user32.dll")]
        public static extern bool GetClientRect(IntPtr hWnd, out _Rect lpRect);

        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();

        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        public static extern bool GetGUIThreadInfo(uint idThread , out _GuiThreadInfo lpgui);

        [DllImport("user32.dll")]
        public static extern IntPtr GetLastActivePopup(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool GetLayeredWindowAttributes(IntPtr hWnd, uint pcrKey, byte pbAlpha, uint pdwFlags);

        [DllImport("user32.dll")]
        public static extern bool GetLayeredWindowAttributes(IntPtr hWnd, uint pcrKey, byte pbAlpha, LayeredWindowAttributesFlags pdwFlags);

        [DllImport("user32.dll")]
        public static extern IntPtr GetNextWindow(IntPtr hWnd, uint wCmd);

        [DllImport("user32.dll")]
        public static extern IntPtr GetNextWindow(IntPtr hWnd, NextWindowCommands wCmd);

        [DllImport("user32.dll")]
        public static extern IntPtr GetParent(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool GetProcessDefaultLayout(out uint pdwDefaultLayout);

        [DllImport("user32.dll")]
        public static extern IntPtr GetShellWindow();

        [DllImport("user32.dll")]
        public static extern uint GetSysColor(int nIndex);

        [DllImport("user32.dll")]
        public static extern uint GetSysColor(SystemColors nIndex);

        [DllImport("user32.dll")]
        public static extern bool GetTitleBarInfo(IntPtr hWnd, out _TitleBarInfo pti);

        [DllImport("user32.dll")]
        public static extern IntPtr GetTopWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern IntPtr GetWindow(IntPtr hWnd , uint uCmd);

        [DllImport("user32.dll")]
        public static extern IntPtr GetWindow(IntPtr hWnd, GetWindowCommands uCmd);

        [DllImport("user32.dll")]
        public static extern bool GetWindowDisplayAffinity(IntPtr hWnd, out uint dwAffinity);

        [DllImport("user32.dll")]
        public static extern bool GetWindowInfo(IntPtr hwnd, ref _WindowInfo pwi);

        [DllImport("user32.dll")]
        public static extern bool GetWindowInfo(IntPtr hWnd, out String lpszFileName, int cchFileNameMax);

        [DllImport("user32.dll")]
        public static extern bool GetWindowPlacement(IntPtr hwnd, out _WindowPlacement lpwndpl);

        [DllImport("user32.dll")]
        public static extern bool GetWindowRect(IntPtr hwnd, out _Rect lpRect);

        [DllImport("user32.dll")]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);

        [DllImport("user32.dll")]
        public static extern int GetWindowTextLength(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

        [DllImport("user32.dll")]
        public extern static int InternalGetWindowText(IntPtr hWnd, String lpString, int nMaxCount);

        [DllImport("user32.dll")]
        public static extern bool IsChild(IntPtr hWndParent, IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool IsGUIThread(bool bConvert);

        [DllImport("user32.dll")]
        public static extern bool IsHungAppWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool IsProcessDPIAware();

        [DllImport("user32.dll")]
        public static extern bool IsIconic(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool IsWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool IsWindowUnicode(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool IsWindowVisible(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool IsZoomed(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool LockSetForegroundWindow(uint uLockCode);

        [DllImport("user32.dll")]
        public static extern bool LockSetForegroundWindow(LockCodes uLockCode);

        [DllImport("user32.dll")]
        public static extern bool LogicalToPhysicalPoint(IntPtr hWnd, ref _Point lpPoint);

        [DllImport("user32.dll")]
        public static extern bool MoveWindow(IntPtr hWnd, int X , int Y , int nWidth , int nHeight , bool bRepaint);

        [DllImport("user32.dll")]
        public static extern bool OpenIcon(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool PhysicalToLogicalPoint(IntPtr hWnd, ref _Point lpPoint);

        [DllImport("user32.dll")]
        public static extern IntPtr RealChildWindowFromPoint(IntPtr hwndParent, _Point ptParentClientCoords);

        [DllImport("user32.dll")]
        public extern static int RealGetWindowClass(IntPtr hWnd, String pszType, uint cchType);

        [DllImport("user32.dll")]
        public static extern bool RegisterShellHookWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public extern static int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        [DllImport("user32.dll")]
        public static extern int ShowWindow(IntPtr hWnd, ShowCommands nCmdShow);

        [DllImport("user32.dll")]
        public static extern int SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool SetLayeredWindowAttributes (IntPtr hWnd, uint crKey, byte bAlpha, LayeredWindowAttributesFlags dwFlags);

        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll")]
        public static extern bool SetProcessDefaultLayout(uint dwDefaultLayout);

        [DllImport("user32.dll")]
        public static extern bool SetProcessDPIAware();

        [DllImport("user32.dll")]
        public static extern bool SetSysColors(int cElements, int[] lpaElements, uint[] lpaRgbValues);

        [DllImport("user32.dll")]
        public static extern bool SetProcessDPIAware(IntPtr hWnd, uint dwAffinity);

        [DllImport("user32.dll")]
        public static extern bool SetWindowPlacement(IntPtr hWnd, _WindowPlacement lpwndpl);

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, PositionFlags uFlags);

        [DllImport("user32.dll")]
        public static extern int SetWindowText(IntPtr hWnd, String lpString);

        [DllImport("user32.dll")]
        public static extern bool SoundSentry();

        [DllImport("user32.dll")]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

        [DllImport("user32.dll")]
        public static extern int TileWindows(IntPtr hwndParent, uint wHow , _Rect lpRect, uint cKids, IntPtr[] lpKids);

        [DllImport("user32.dll")]
        public static extern bool UpdateLayeredWindow(IntPtr hWnd, IntPtr hdcDst, _Point pptDst, _Size psize, IntPtr hdcSrc, _Point pptScr, uint crKey, _BlendFunction pblend, uint dwFlags);

        [DllImport("user32.dll")]
        public static extern bool UpdateLayeredWindow(IntPtr hWnd, IntPtr hdcDst, _Point pptDst, _Size psize, IntPtr hdcSrc, _Point pptScr, uint crKey, _BlendFunction pblend, UpdateLayeredWindowFlags dwFlags);

        [DllImport("user32.dll")]
        public static extern IntPtr WindowFromPhysicalPoint(_Point Point);

        [DllImport("user32.dll")]
        public static extern IntPtr WindowPoint(_Point Point);

        [DllImport("user32.dll")]
        public static extern int WinMain(IntPtr hInstance, IntPtr hPrevInstance , String lpCmdLine , int nCmdShow);

        [DllImport("user32.dll")]
        public static extern int WinMain(IntPtr hInstance, IntPtr hPrevInstance, String lpCmdLine, ShowCommands nCmdShow);

        #endregion

        #region shell32.dll

        [DllImport("shell32.dll")]
        public static extern IntPtr CopyIcon(IntPtr hicon);

        [DllImport("shell32.dll")]
        public static extern IntPtr CreateIcon(IntPtr hInstance, int nWidth , int nHeight , byte cPanels, byte cBitsPixel , byte[] lpbANDbits, byte[] lpbXORbits);

        [DllImport("shell32.dll")]
        public static extern IntPtr CreateIconFromResource(byte[] presbits, uint dwResSize, bool fIcon, uint dwVer);

        [DllImport("shell32.dll")]
        public static extern IntPtr CreateIconFromResourceEx(byte[] presbits, uint cbIconBits, bool fIcon, uint dwVe, int cxDesired, int cyDesired, uint uFlags);

        [DllImport("shell32.dll")]
        public static extern IntPtr CreateIconFromResourceEx(byte[] presbits, uint cbIconBits, bool fIcon, uint dwVe, int cxDesired, int cyDesired, CreateIconFromResourceFlags uFlags);

        [DllImport("shell32.dll")]
        public static extern IntPtr CreateIconIndirect(_IconInfo piconinfo);

        [DllImport("shell32.dll")]
        public static extern bool DestroyIcon(IntPtr hicon);

        [DllImport("shell32.dll")]
        public static extern bool DrawIcon(IntPtr hDC, int x, int y, IntPtr hIcon);

        [DllImport("shell32.dll")]
        public static extern bool DrawIcon(IntPtr hDC, int xLeft, int yTop, IntPtr hIcon, int cxWidth, int cyWidth, uint istepIfAniCur, IntPtr hbrFlickerFreeDraw , uint diFlags);

        [DllImport("shell32.dll")]
        public static extern bool DrawIcon(IntPtr hDC, int xLeft, int yTop, IntPtr hIcon, int cxWidth, int cyWidth, DrawIconFlags istepIfAniCur, IntPtr hbrFlickerFreeDraw, uint diFlags);

        [DllImport("shell32.dll")]
        public static extern IntPtr DuplicateIcon(IntPtr hInst, IntPtr hIcon);

        [DllImport("shell32.dll")]
        public static extern IntPtr ExtractAssociatedIcon(IntPtr hInst, StringBuilder lpIconPath, out ushort lpiIcon);

        [DllImport("shell32.dll")]
        public static extern IntPtr ExtractIcon(IntPtr hInst, String lpszExeFileName, uint nIconIndex);

        [DllImport("shell32.dll")]
        public static extern uint[] ExtractIconEx(String lpszFile, uint nIconIndex, out IntPtr[] phiconLarge, out IntPtr[] phiconSmall , uint nIcons);

        [DllImport("shell32.dll")]
        public static extern bool GetIconInfo(IntPtr hicon, out _IconInfo piconinfo);

        [DllImport("shell32.dll")]
        public static extern bool GetIconInfoEx(IntPtr hicon, ref _IconInfo piconinfoex);

        [DllImport("shell32.dll")]
        public static extern bool LoadIcon(IntPtr hInstance, String lpIconName);

        [DllImport("shell32.dll")]
        public static extern int LookupIconIdFromDirectory(byte[] presbits, bool fIcon);

        [DllImport("shell32.dll")]
        public static extern int LookupIconIdFromDirectoryEx(byte[] presbits, bool fIcon, int cxDesired , int cyDesired , uint Flags);

        [DllImport("shell32.dll")]
        public static extern int LookupIconIdFromDirectoryEx(byte[] presbits, bool fIcon, int cxDesired, int cyDesired, LookupIconIdFromDirectoryFlags Flags);

        [DllImport("shell32.dll")]
        public static extern uint GetIconInfo(String lpszFile, int nIconIndex, int cxIcon, int cyIcon , out IntPtr[] phicon , out uint piconid, uint nIcons , uint Flags);

        #endregion

        #endregion

        #region bearbeitet

        public static int GetWindowThreadProcessId(IntPtr hWnd)
        {
            int pid = 0;
            GetWindowThreadProcessId(hWnd, out pid);
            return pid;
        }

        public static String GetWindowText(IntPtr hWnd)
        {
            // Gibt den Title eines Fensters zurück.

            StringBuilder Title = new StringBuilder(255);                                   // 255 = Maximale Länge des Titles
            int Length = Win32Helper.GetWindowText(hWnd, Title, Title.Capacity + 1);        // Tatsächliche Länge des Title
            Title.Length = Length;                                                          // Damit keine Leerzeichen angezeigt werden.
            return Title.ToString();
        }

        public static _WindowInfo GetWindowInfo(IntPtr hWnd)
        {
            _WindowInfo info = new _WindowInfo();
            GetWindowInfo(hWnd, ref info);
            return info;
        }

        #endregion
    }
}
